<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>

      <el-table class="mt-20" :data="list">
        <el-table-column prop="id" label="序号" />
        <el-table-column prop="sonId" label="用户id" />
        <el-table-column prop="num" label="消费金额" />
        <el-table-column prop="parentId" label="获取积分提成的推荐人id" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="status" label="状态" />
        <el-table-column prop="text" label="操作" width="200" fixed="right" >
          <template slot-scope="scope" >
            <el-button size="small" type="primary" @click="handleEdit(scope.row,scope.$index)">分发奖励</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </el-tabs>
      </el-card> 
    </div>
    
    <!-- 分页 -->
    <div align="center">
      <el-pagination
        :current-page.sync="currentPage"
        layout="prev, pager, next, jumper"
        :total="total"
        size-change
      />
    </div>
    <!-- 编辑/新建 弹窗 -->
    <el-dialog :visible="showEdit" @close="showEdit=false;formData={}" label-width="400px">
      <el-form label-width="260px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="formData.userId"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="积分类型" prop="type">
          <el-input v-model="formData.type"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="积分来源" prop="isource">
          <el-input v-model="formData.isource"  placeholder="请输入" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="formData.number"  placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="状态" prop="istate">
          <el-switch
            v-model="formData.istate"
            :active-text="formData.istate?'正常':'无效'"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
        
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showEdit=false">取消</el-button>
          <el-button type="primary" @click="editSubmit">确认</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 删除确认框 -->
    <el-dialog title="提示" :visible.sync="deleteVisible" width="30%">
      <span>确定删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteComfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {SelectAll,Delete,Uadept} from "@/api/permission";
export default {
  data() {
    return {
      list: [],
      pageParam: { // 分页参数对象
      
      },
      pageSize: 10,
      formData: {},
      showEdit: false,
      deleteVisible: false,
      editIndex:null,
      deleteRow:null
    };
  },
  async mounted() {
    // console.log(service)
    // swlService.select();
    this.getList()
  },
  methods: {
    async getList(){
      const data = await SelectAll()
      this.list = data
      console.log('请求响应结果', data)

      // this.$set(this.list,res)
      res.map((item)=>{
          item.statusName = item.state===1?'正常':'无效'
          return item
      })
      this.list = res;
      this.list = this.list.filter(item => item)
      this.total = res.length
    },
    handleEdit(row,index) {
      console.log(index)
      this.formData = row;
      this.showEdit = true;
      this.editIndex=index
    },
    handleDelete(row) {
      this.deleteRow = row
      this.deleteVisible = true
    },
    // 删除
    async deleteComfirm(){
      const res = await Delete({
        id: this.deleteRow.id
      })
      this.$message.success(res)
      this.deleteVisible=false
      this.getList();
    },
    async editSubmit() {
      let res = ''
      if (this.formData.id) {
        //编辑
        this.formData.status=this.formData.status?'有效':'无效'
        res=await Uadept(this.formData)
      } 
      this.getList()
      this.formData={}
      this.$message.success(res)
      this.showEdit=false
    }
  }
};
</script>